// Copyright (C) Jari Hautio <jari.hautio@iki.fi> 2008. Licensed under GPLv2. See LICENSE.txt in solution folder.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RyijyApp
{
    public partial class ColorForm : Form
    {
        private string searchPrompt="";
        private CustomPalette colorPalette = null;

        internal CustomPalette ColorPalette
        {
            get { return colorPalette; }
            set { colorPalette = value; UpdateColors(); }
        }
        private Color selectedColor;

        public Color SelectedColor
        {
            get { return selectedColor; }
            set { selectedColor = value; }
        }
	
	
        public ColorForm()
        {
            InitializeComponent();            
        }
        void UpdateColors()
        {
            listView1.Items.Clear();
            if (colorPalette == null) return;

            foreach (Color c in colorPalette.GetColors())
            {
                ListViewItem li = new ListViewItem(new string[] { colorPalette.GetName(c), colorPalette.GetText(c) });
                li.BackColor = c;
                if (li.BackColor.GetBrightness() < 0.4)
                    li.ForeColor = Color.White;
                li.Tag = c;
                listView1.Items.Add(li);
            }
        }

        private void ColorForm_Load(object sender, EventArgs e)
        {
            UpdateColors();
            button1.Enabled = false;
            searchPrompt = textBoxFind.Text;
            textBoxFind.ForeColor = Color.FromKnownColor(KnownColor.GrayText);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = listView1.SelectedItems.Count > 0;
            if (listView1.SelectedItems.Count > 0)
            {
                labelColor.BackColor = listView1.SelectedItems[0].BackColor;
                labelColor.Text = listView1.SelectedItems[0].Text;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
                selectedColor = listView1.SelectedItems[0].BackColor;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBoxFind.ForeColor == Color.FromKnownColor(KnownColor.WindowText))
            {
                ListViewItem it = listView1.FindItemWithText(textBoxFind.Text);
                if (it!= null) it.Selected = true;
            }
        }

        private void textBoxFind_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
        }

        private void textBoxFind_TextChanged(object sender, EventArgs e)
        {
            if (textBoxFind.ForeColor == Color.FromKnownColor(KnownColor.WindowText))
            {
                ListViewItem it = listView1.FindItemWithText(textBoxFind.Text);
                if (it != null)
                {
                    foreach (ListViewItem i in listView1.SelectedItems)
                        i.Selected = false;
                    it.Selected = true;
                    it.EnsureVisible();
                }
            }
        }

        private void textBoxFind_Enter(object sender, EventArgs e)
        {
            if (textBoxFind.ForeColor == Color.FromKnownColor(KnownColor.GrayText))
            {
                textBoxFind.Text = "";
                textBoxFind.ForeColor = Color.FromKnownColor(KnownColor.WindowText);
            }

        }

        private void textBoxFind_Leave(object sender, EventArgs e)
        {
            if (textBoxFind.Text.Length == 0)
            {
                textBoxFind.ForeColor = Color.FromKnownColor(KnownColor.GrayText);
                textBoxFind.Text = searchPrompt;
            }
        }

        private void labelColor_Click(object sender, EventArgs e)
        {
            ListViewItem it = listView1.FindItemWithText(labelColor.Text, false, 0, false);
            if (it!= null)
            {
                foreach (ListViewItem i in listView1.SelectedItems)
                    i.Selected = false;
                it.Selected = true;
                it.EnsureVisible();
            }
        }
    }
}